
#include <stdint.h>
#include  "reg.h"

#include <initcall.h>

//#include "tim_reg.h"


#define  TIM6_ADDR    		0x40001000 
#define  TIM6      			(*(volatile uint32_t *)(TIM6_ADDR))
#define  TIM6_CR1     		(*(volatile uint32_t *)(TIM6_ADDR+0x00))
#define  TIM6_CR2     		(*(volatile uint32_t *)(TIM6_ADDR+0x04))
#define  TIM6_DIER    		(*(volatile uint32_t *)(TIM6_ADDR+0x0C))
#define  TIM6_SR  			(*(volatile uint32_t *)(TIM6_ADDR+0x10))
#define  TIM6_EGR 			(*(volatile uint32_t *)(TIM6_ADDR+0x14))
#define  TIM6_CNT			(*(volatile uint32_t *)(TIM6_ADDR+0x24))
#define  TIM6_PSC			(*(volatile uint32_t *)(TIM6_ADDR+0x28))
#define  TIM6_ARR			(*(volatile uint32_t *)(TIM6_ADDR+0x2C))



void tim6_init(){
	
	RCC_APB1ENR1  |=  1 << 4; //tim6 clk enable;
	
	TIM6_PSC =  40-1;     //0; //no scale  40 mhz apb bus frequency.
	TIM6_ARR = 0xffff;  //can count to 0xffff ns
		
	TIM6_CR1  |= (1<<0);  //enable count
	while(    !(TIM6_SR & (1 <<0)) );   //wait hardware enabled
}
dev_init(tim6_init);

void tim6_delay_ns(uint32_t count){

	TIM6_CNT = 0;
	while(TIM6_CNT < count);
}

void tim6_delay_ms(uint32_t count){
       while(count--)
	tim6_delay_ns(1000);
}

void TIM6_DAC_IRQHandler(void ){
	my_fputs("tim6  triggered\r\r");
	
}
